package web

import (
	"github.com/gin-gonic/gin"
	"gquickstarter/internal/service"
	_interface "gquickstarter/internal/web/interface"
	"net/http"
)

type AccountController struct {
	service service.IAccountService
}

var _ _interface.Controller = (*AccountController)(nil)

func CreateAccountController(svc service.IAccountService) *AccountController {
	return &AccountController{
		service: svc,
	}
}
func (controller *AccountController) RegisterRoute(engine *gin.RouterGroup) {
	engine.POST("/login", controller.Login)
	engine.POST("/register", controller.Register)
	engine.POST("/info", controller.Information)
}

func (controller *AccountController) Login(ctx *gin.Context) {

}

// Register 注册功能
func (controller *AccountController) Register(ctx *gin.Context) {
	type RegisterReq struct {
		Email      string `json:"Email"`
		Password   string `json:"password"`
		RePassword string `json:"re-password"`
	}
	var req = RegisterReq{}
	err := ctx.Bind(&req)
	if err != nil {
		ctx.JSON(http.StatusOK, Result{
			Code:    0,
			Message: "系统错误",
		})
	}
	err = controller.service.RegisterByEmail(ctx, req.Email, req.Password)
	if err != nil {
		ctx.JSON(http.StatusOK, Result{
			Code:    40020,
			Message: "注册失败",
		})

	} else {
		ctx.JSON(http.StatusOK, Result{
			Code:    20020,
			Message: "注册成功",
		})
	}
}
func (controller *AccountController) Information(ctx *gin.Context) {

}
